//
//  ChooseMapLayer.m
//  CarDemo
//
//  Created by huang kun on 8/9/10.
//  Copyright 2010 UESTC. All rights reserved.
//

#import "ChooseMapLayer.h"
#import "GameLayer.h"


@implementation ChooseMapLayer

@synthesize mapIndex;

- (void) dealloc {
	[self removeAllChildrenWithCleanup:YES];
	[super dealloc];
}

- (id) init {
	if( (self = [super init]) ){
		isTouchEnabled = YES;
		CGSize winSize = [[CCDirector sharedDirector] winSize];
		
		
		CCSprite *bg = [CCSprite spriteWithFile:@"chooseMapBg.png"];
		[self addChild:bg];
		[bg setPosition:ccp(winSize.width/2, winSize.height/2)];
		
		
		CCMenuItemImage *button1 = [CCMenuItemImage itemFromNormalImage:@"map1.png" selectedImage:@"map1_2.png" 
																	 target:self selector:@selector(didSelect:)];
		CCMenuItemImage *button2 = [CCMenuItemImage itemFromNormalImage:@"map2.png" selectedImage:@"map2_2.png"
																		target:self selector:@selector(didSelect:)];
		CCMenuItemImage *button3 = [CCMenuItemImage itemFromNormalImage:@"map3.png" selectedImage:@"map3_2.png"
																	  target:self selector:@selector(didSelect:)];
		CCMenuItemImage *button4 = [CCMenuItemImage itemFromNormalImage:@"map4.png" selectedImage:@"map4_2.png"
																	 target:self selector:@selector(didSelect:)];
		CCMenuItemImage *button5 = [CCMenuItemImage itemFromNormalImage:@"map5.png" selectedImage:@"map5_2.png"
																	target:self selector:@selector(didSelect:)];
		
		CCMenu *menu = [CCMenu menuWithItems:button1,button2,button3,button4,button5,nil];
		[self addChild:menu z:1];
		[menu setPosition:CGPointZero];
		
		[button1 setPosition:ccp(89, 235)];
		[button2 setPosition:ccp(284, 261)];
		[button3 setPosition:ccp(408, 256)];
		[button4 setPosition:ccp(284, 126)];
		[button5 setPosition:ccp(450, 76)];
		
		[button1 setTag:1];
		[button2 setTag:2];
		[button3 setTag:3];
		[button4 setTag:4];
		[button5 setTag:5];
	}
	return self;
}

- (void) didSelect:(id)sender {
	CCMenuItemImage *item = (CCMenuItemImage *)sender;
	switch (item.tag) {
		case 1:
		{
		}
			break;
		case 2:
		{
		}
			break;
		case 3:
		{
		}
			break;
		case 4:
		{
			[self startGame];
		}
			break;
		case 5:
		{
		}
			break;
		default:
			break;
	}
}
/*
- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
	CGPoint location = [touch locationInView:[touch view]];	
	location = [[CCDirector sharedDirector] convertToGL:location];
	
	if( CGRectContainsPoint(sprite1.boundingBox, location) ){
		mapIndex = 1;
		[self startGame];
	}
	else if( CGRectContainsPoint(sprite2.boundingBox, location) ){
		mapIndex = 2;
		[self startGame];
	}
	else if( CGRectContainsPoint(sprite3.boundingBox, location) ){
		mapIndex = 3;
		[self startGame];
	}
	else if( CGRectContainsPoint(sprite4.boundingBox, location) ){
		mapIndex = 4;
		[self startGame];
	}
	else if( CGRectContainsPoint(sprite5.boundingBox, location) ){
		mapIndex = 5;
		[self startGame];
	}
}
 */

- (void) startGame {
	[[CCTextureCache sharedTextureCache] removeUnusedTextures];
	[[CCTextureCache sharedTextureCache] removeAllTextures];
	[[CCDirector sharedDirector] replaceScene:[[CCScene node] addChild:[GameLayer node]]];
}

@end
